﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JQGridMVC.Repository;
using System.Reflection;

namespace Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Browse Profiles";

            return View();
        }

        public ActionResult About(int Id = 0)
        {
            ViewBag.Message = "Create Profile";
            FakeProfile profile = null;
            if (Id != 0)
            {
                var repo = new AppRepository();
                profile = repo.FakeProfiles.FirstOrDefault(e => e.Id == Id);
            }
            return View(profile);
        }

        [HttpPost]
        public ActionResult About(FakeProfile FakeProfile)
        {
            var buttonClicked = Request.Form["submit-button"];
            if (buttonClicked == "Submit")
            {
                using (var AppRepository = new AppRepository())
                {
                    if (FakeProfile.Id == 0)
                    {
                        AppRepository.FakeProfiles.Add(FakeProfile);
                    }
                    else
                    {
                        var entity = AppRepository.FakeProfiles.First(e => e.Id == FakeProfile.Id);
                        entity.FirstName = FakeProfile.FirstName;
                        entity.LastName = FakeProfile.LastName;
                        entity.PhoneNumber = FakeProfile.PhoneNumber;
                        entity.Email = FakeProfile.Email;
                    }
                    AppRepository.SaveChanges();
                }
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }

        }

        public JsonResult GetProfiles(int page = 1, int rows = 10, string sord = "asc", string sidx = "Id")
        {
            var repository = new AppRepository();
            var recordCount = repository.FakeProfiles.Count();
            IEnumerable<FakeProfile> recordsForPage;
            IEnumerable<object> final;
            IEnumerable<FakeProfile> fakeProfiles = (IEnumerable<FakeProfile>)repository.FakeProfiles;
            var typeOfFakeProfiles = fakeProfiles.GetType();
            var fakeProfileType = typeof(FakeProfile);
            var propertyInfo = fakeProfileType.GetProperty(sidx);
            Func<FakeProfile, object> expn = e => propertyInfo.GetValue(e, null);
            if (sord == "asc")
            {
                recordsForPage = Enumerable.OrderBy(fakeProfiles, expn).Skip((page - 1) * rows).Take(rows).ToArray();
            }
            else
            {
                recordsForPage = Enumerable.OrderByDescending(fakeProfiles, expn).Skip((page - 1) * rows).Take(rows).ToArray();
            }
            final = recordsForPage.Select(e => new
            {
                Id = e.Id,
                FirstName = e.FirstName,
                LastName = e.LastName,
                PhoneNumber = e.PhoneNumber.ToString(),
                Email = e.Email
            }).ToArray();
            return Json(new
            {
                total = Math.Ceiling((Decimal)recordCount / (Decimal)rows),
                page = page,
                records = recordCount,
                rows = final
            }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DeleteProfile(int Id, int RowId)
        {
            var repository = new AppRepository();
            var entity = repository.FakeProfiles.FirstOrDefault(e => e.Id == Id);
            repository.FakeProfiles.Remove(entity);
            repository.SaveChanges();
            return Json(new { Success = true, RowId = RowId }, JsonRequestBehavior.AllowGet);
        }
    }
}
